package com.hfh.main.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hfh.main.common.method.commonMethod;
import com.hfh.main.group.entity.GroupDetailEntity;
import com.hfh.main.group.entity.GroupEntity;
import com.hfh.main.group.mapper.GroupDetailMapper;
import com.hfh.main.group.mapper.GroupMapper;
import com.hfh.main.report.entity.WeekEntity;
import com.hfh.main.report.mapper.WeekMapper;
import com.hfh.main.user.entity.UserEntity;
import com.hfh.main.user.mapper.UserMapper;
import com.hfh.main.user.service.UserService;
import com.hfh.main.weight.entity.ScoreWeightEntity;
import com.hfh.main.weight.mapper.ScoreWeightMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 人员表
 * </p>
 *
 * 
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WeekMapper weekMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private GroupDetailMapper groupDetailMapper;
    @Autowired
    private ScoreWeightMapper scoreWeightMapper;

    @Override
    public UserEntity queryByAcc(String param) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("user_acc"), param);
        UserEntity userEntity = userMapper.selectOne(wrapper);
        return userEntity;
    }
    @Override
    public HashMap queryShowInfoByAcc(String param) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("user_acc"), param);
        UserEntity userEntity = userMapper.selectOne(wrapper);
        HashMap showInfo = null;
        try {
            showInfo = (HashMap) commonMethod.objectToMap(userEntity);
            //移除pwd
            showInfo.remove("pwd");
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return showInfo;
    }

    public Boolean updateUserByAcc(UserEntity entity) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("user_acc"), entity.getAcc());
        return userMapper.update(entity, wrapper)>0;
    }

    @Override
    public Boolean updateRoleByAcc(UserEntity entity) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("user_acc"), entity.getAcc());
        entity.setRole(2);
        return userMapper.update(entity, wrapper)>0;
    }

    /**
     * 通过评分标准 计算最终得分
     */
    @Override
    public Double calculateFinalScore(String acc) {

        //拿日常得分 即所有周的组长评分
        List<WeekEntity> weekEntity = weekMapper.selectList(new QueryWrapper<WeekEntity>().eq("user_acc", acc));
        //所有周报内的weekscore均不为null 或 0
        weekEntity.forEach(week -> {
            if (week.getWeekScore() == null || week.getWeekScore() == 0) {
                //得到用户信息
                QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
                wrapper.eq(("user_acc"), acc);
                UserEntity userEntity = userMapper.selectOne(wrapper);
                throw new RuntimeException(userEntity.getName() + " "+userEntity.getAcc()+" 第 "+week.getWeekNumber()+" 周的周报未填写完整！");
            }
        });

        //得到所有周的总分
        Double totolScore = weekEntity.stream().mapToDouble(WeekEntity::getWeekScore).sum();
        totolScore = totolScore/28 * 100; //个人四周得分/28*100

        //得到一二阶段及中期终期答辩分
        GroupDetailEntity gdE = groupDetailMapper.selectOne(new QueryWrapper<GroupDetailEntity>().eq("user_acc", acc));
        String groupId = String.valueOf(gdE.getGroupId());
        GroupEntity groupEntity = groupMapper.selectOne(new QueryWrapper<GroupEntity>().eq("group_id", groupId));
        Double dbFirst = groupEntity.getDbFirst();
        Double dbSecond = groupEntity.getDbSecond();
        Double dbMid = groupEntity.getDbMid();
        Double dbTerminal = groupEntity.getDbTerminal();

        //拿到打分权重
        ScoreWeightEntity sw = scoreWeightMapper.selectById(1);
        if(sw != null){
            //通过评分标准 计算最终得分
            Double finalScore = ( (totolScore * sw.getDailyWeight())
                    + (dbFirst * sw.getOneWeight())
                    + (dbSecond * sw.getOneWeight())
                    + (dbMid * sw.getTwoWeight())
                    + (dbTerminal * sw.getFinalWeight())
                    ) * 0.01 ;

            //精确到一位有效数字 老师有要求
            finalScore = Double.parseDouble(String.format("%.1f", finalScore));
            return finalScore;
        }
        return null;
    }
}
